package sk.ownage.p1.featurestore;

import java.io.File;
import java.util.Vector;

import android.os.Environment;
import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

public class LandmarkGeometry {
	Vector<LandmarkQuadTreeRoot> QTreeRoots=new Vector<LandmarkQuadTreeRoot>();
	Vector<Vector<PointOfInterestsInfo>> markerOptionsList=new Vector<Vector<PointOfInterestsInfo>>();
	Vector<LandmarkGeometryThread> threads=new Vector<LandmarkGeometry.LandmarkGeometryThread>();
	int landmarkId;
	private String landmarkRootPath="";
	final String TAG="LandmarkGeometry";
	LandmarkReader lr;
	String path;
	
	public LandmarkGeometry(int landmarkId){
		this(landmarkId,"");
	}
	
	public LandmarkGeometry(int landmarkId, String landmarkRootPath){
		this.landmarkRootPath=landmarkRootPath;
		this.landmarkId=landmarkId;
		int folderId = this.landmarkId / LandmarkQuadTreeRoot.DIRECTORY_MAX;
		int fileId = this.landmarkId % LandmarkQuadTreeRoot.DIRECTORY_MAX;
		String folderPath = (((folderId / 100) > 0) ? "" : "0")
				+ (((folderId / 10) > 0) ? "" : "0") + folderId;
		String filePath = (((fileId / 100) > 0) ? "" : "0")
				+ (((fileId / 10) > 0) ? "" : "0") + fileId;
		path =((!this.landmarkRootPath.equals(""))?this.landmarkRootPath:LandmarkQuadTreeRoot.rootPath) + "/" + folderPath + "/" + filePath + "/" ;

		for(int i=0;i<5;i++){
			QTreeRoots.add(new LandmarkQuadTreeRoot(landmarkId,path, i));
			threads.add(new LandmarkGeometryThread(i));
			markerOptionsList.add(new Vector<PointOfInterestsInfo>());
		}
		
		File landmarkFile = new File(Environment.getExternalStorageDirectory()
				+ "/featureStore" + "/landmarks" + "/" + folderPath + "/"
				+ filePath + ".bin");
		File geomSize=new File(path+"4/size.bin");
		long last_modified=QTreeRoots.get(4).last_landmark_timestamp;
		boolean addNewPoints=true;
		if(geomSize.exists() && landmarkFile.exists())
			addNewPoints=(landmarkFile.lastModified()>geomSize.lastModified());
		
		if (addNewPoints) {
			String temp=path.substring(0, path.lastIndexOf("/geometry"));
			lr = new LandmarkReader(landmarkId,(!this.landmarkRootPath.equals(""))?temp + "/landmarks":"");
			try {
				lr.readName();
				lr.readCategory();
				lr.readTimestamp();
				lr.readDescription();
				while (lr.hasLandmark()) {
					LatLng position=new LatLng(lr.readLandmarkLatitude(), lr.readLandmarkLongitude());
					lr.readLandmarkAltitude();
					long pointTimestamp=lr.readLandmarkTimestamp();
					if(pointTimestamp>last_modified){
					this.addPointOfInterest(position,pointTimestamp);
					}
				}
			} catch (Exception e) {
				Log.d(TAG, e.toString());
			}
		}
		
	}
	
	public void addPointOfInterest(LatLng position, long timestamp){
		for(int i=0;i<5;i++){
			markerOptionsList.get(i).add(new PointOfInterestsInfo(position,timestamp));
		if(!threads.get(i).isAlive()){
			threads.set(i, new LandmarkGeometryThread(i));
			threads.get(i).start();
			}
		}
	}
	
	private class LandmarkGeometryThread extends Thread{
		int threadNum=0;
		
		public LandmarkGeometryThread(int _threadNumber){
			threadNum=_threadNumber;
		}
		@Override
		public void run() {
			
			while (markerOptionsList.get(threadNum).size()>0) {
				QTreeRoots.get(threadNum).addPointOfInterestToLandmarkGeometry(markerOptionsList.get(threadNum).get(0));
				markerOptionsList.get(threadNum).remove(0);
			}
		}
		
	}

}

